<?php

class CHotSearch extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	
	public function __construct(){
		
		$this->_name=R_PRE . $type . "hotsearch";
		$this->_primary = "id";
		
		parent::__construct();
		
	}
	
	public function createHotSearch($data){
		
		try{
			$this->insert($data);

			return true;
			
		}catch(Exception $e){
			
			$e;
			
		}
		
		return false;
		
	}
	/**
	public function getHotSearch($fieldArray=null){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'COUNT(*)');
		
		return $db->fetchAll($select);
		
		
	}**/
	
	public function getHotSearch($count = 10)
	{
		$sql = 'select distinct keyword from ' . $this->_name . ' limit ' . $count;
		$row = $this->getAdapter()->fetchAll($sql);
		foreach ($row as $v) {
			$arr[] = '<a href="'.MY_URL.'/soccer-jerseys/' .$this->encode($v['keyword']). '/">' . $v['keyword'] . '</a>';
		}
		$arr = array_unique($arr);
		return implode(', ', $arr);
	}
	
	public function getAll($page, $pageNum)
	{
		$where = array();
		
		$sql = 'select keyword from ' . $this->_name;
		
		$offset = ($page - 1) * $pageNum;
		
		$sql .= ' order by id asc limit ' . $offset . ',' . $pageNum;
	
		return  $this->getAdapter()->fetchAll($sql);
	}
	
	public function encode($name, $size = 0)
	{
		$name = str_replace('/', ' ', $name);
		$name = str_replace('\\', ' ', $name);
		return rawurlencode(str_replace(' ', '-', $name));
	}
	
	public function delHotSearch($field,$valueArray){
		
		$db=$this->getAdapter();
		
		$valueString=join(',',$valueArray);

		$db->delete($this->_name,"$field IN ($valueString)");
		
	}
	
	public function getAllItemCounts($where = '')
	{
		$db=$this->getAdapter();

        $select = $db->select();

        $select->from($this->_name);

        if($where!='')
        {
            $select->where($where);
        }

        return count($db->fetchAll($select));
	}
	
	public function hslist($from, $counts, $where='')
	{
		$db=$this->getAdapter();

        $itemTable=$this->_name;

        $select = $db->select();
        $select->from($itemTable,'*');

        if($where!='')
        {
            $select->where($where);
        }

        if($counts!=0)
        {

            $select->limit($counts, $from);

        }
        return $db->fetchAll($select);
	}

	public function getDetail($id)
	{
		$db=$this->getAdapter();

        $select = $db->select();

        $select->from($this->_name,'*');


        $select->where('id = ? ',$id);

        return $db->fetchAll($select);
	}
	
	public function editHotSearch($id, $data)
	{
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('id = ?', $id);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
	}
	
	public function hsdelete($id)
	{
		$db=$this->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
		return $this->delete($where);
	}
	
	public function getDisplay()
	{
		$db=$this->getAdapter();

        $select = $db->select();
		$select->limit(30,1);
		$select->order('click desc');
        $select->from($this->_name);

        return $db->fetchAll($select);
	}
	
	public function updateView($keywords)
	{
		$where = $this->getAdapter()->quoteInto('keyword = ?', $keywords);
		return $this->update(array('click'=>new Zend_Db_Expr('click+1')), $where);
	}
	
	public function getRandomKeywords($num,$keyword){
		$myCache = Zend_Registry::get('myCache');
		$count = $myCache->load('chot_keywordsCount');
		if($count == 0 || !$count){
			$sql = 'select max(id) from ' . $this->_name;
			$count = $this->getAdapter()->fetchOne($sql);
			$myCache->save($count,'chot_keywordsCount');
		}
		$ids = array();
		$md5Keyword = md5($keyword);
		for($i=0;$i<$num;$i++){
			$md5Keyword = md5(md5($md5Keyword).md5(pow($i,10))).md5($md5Keyword.$i);
			$value = '';
			for($j=0,$len=strlen($md5Keyword);$j<$len;$j++){
				$value.=ord($md5Keyword[$j]);
			}
			$value = substr($value,$i,10);
			$rid=($value+8682522807148012)%$count;
			$ids[]=$rid;
		}
		$idStr = "'".join("','",$ids)."'";
		$sql = 'select keyword from '.$this->_name.' where id in('.$idStr.')';
		return $this->getAdapter()->fetchAll($sql);	
	}
	/**
	public function getHotSearch($count = 10, $start = 0, $top = 1)
	{
		$sql = 'select distinct keyword from ' . $this->_name . ' order by cilck limit ' . $start . ',' . $count;
		$row = $this->getAdapter()->fetchAll($sql);
		foreach ($row as $v) {
			$arr[] = '<a href="/soccer-jerseys/' .$this->encode($v['keyword']). '/">' . $v['keyword'] . '</a>';
		}
		$arr = array_unique($arr);
		if ($top) {
			return implode(', ', $arr);
		}
		return $arr;
	}
	
	
	public function getDetailPopKey($productId)
	{
		$sql = 'select distinct(k.keyword) from zcshop_product_keywords_related p inner join zcshop_keywords k on k.keywords_id=p.keywords_id where p.productid=' . intval($productId) . ' and k.product_num > 5 order by k.view_num desc limit 0, 8';
		$result = $this->getAdapter()->fetchAll($sql);
		$arr = array();
		foreach ($result as $v) {
			$arr[] = '<a href="/soccer-jerseys/' .$this->encode($v['keyword']). '/">' . $v['keyword'] . '</a>';
		}
		$arr = array_unique($arr);
		return implode(', ', $arr);
	}**/
	
	public function clickHotSearch($id)
	{
		$db=$this->getAdapter();
		
//		$where = $db->quoteInto('id = ?', $id);
		
		//$this->_name,array('click'=>'click+1'),$where
		$rows_affected=$db->fetchOne("update $this->_name set click=click+1 where id = $id");
//		$rows_affected=$db->update($this->_name,array('click'=>'click+1'),$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
	}
}